package masterjava.common.exception;

import masterjava.common.configuration.AppSetting;
import masterjava.common.logging.MasterLogger;
import masterjava.common.type.FaultInfoType;
import masterjava.common.ws.WebServiceFault;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

/**
 * User: GKislin
 * Date: 14.01.2011
 */
public class FaultFactory {
    private static final MasterLogger LOGGER = MasterLogger.get(FaultFactory.class);

    private FaultFactory() {
    }

    public static WebServiceFault createFault(FaultInfo faultInfo) {
        return createFault(faultInfo, null, 0, null, null);
    }

    public static WebServiceFault createFault(FaultInfo faultInfo, String detail, List<String> context) {
        return createFault(faultInfo, detail, 0, context, null);
    }

    public static WebServiceFault createFault(FaultInfo faultInfo, int code, String detail) {
        return createFault(faultInfo, detail, code, null, null);
    }

    public static WebServiceFault createFault(FaultInfo faultInfo, Exception e) {
        return createFault(faultInfo, null, 0, null, null);
    }

    public static WebServiceFault createFault(FaultInfo faultInfo, String detail, int code, List<String> context, Throwable cause) {
        FaultInfoType type = faultInfo.getFaultInfo();
        if (detail == null && cause != null) {
            detail = cause.toString();
        }
        type.setDetail(detail);
        type.setCode(code);
        if (CollectionUtils.isNotEmpty(context)) {
            type.getContext().addAll(context);
        }
        type.setSource(AppSetting.get().APP_ID.get());
        type.setVcsRevision(AppSetting.get().VSC_REVISION.get());

        LOGGER.error(faultInfo.getMessage() + ": " + faultInfo.getFaultInfo().toString(), cause);
        return new WebServiceFault(faultInfo.getMessage(), type, cause);
    }

}
